/**
 * @author Jorge Mejuto Created on 17-mar-2005
 */
package net.eyra.utiles;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Zip {

    public void guardarZip(String path_zip, String[] path_ficheros, boolean comprimido) throws FicheroException {
        ArrayList<String> paths = new ArrayList<String>(path_ficheros.length);

        for (int x = 0; x < path_ficheros.length; x++) {
            if (path_ficheros[x] != null) paths.add(path_ficheros[x]);
        }

        paths.trimToSize();
        path_ficheros = new String[paths.size()];

        String[] nombre_ficheros = new String[paths.size()];

        for (int x = 0; x < paths.size(); x++) {
            StringTokenizer path = new StringTokenizer(paths.get(x), System.getProperty("file.separator"));
            path_ficheros[x] = paths.get(x);
            while (path.hasMoreElements())
                nombre_ficheros[x] = path.nextToken();
        }

        guardarZip(path_zip, path_ficheros, nombre_ficheros, comprimido);
    }

    public void guardarZip(String path_zip, String[] path_ficheros, String[] nombre_ficheros, boolean comprimido) throws FicheroException {
        final int BUFFER = 2040;

        BufferedInputStream origen = null;
        FileOutputStream destino;
        try {
            destino = new FileOutputStream(path_zip + ".zip");
            ZipOutputStream salida = new ZipOutputStream(new BufferedOutputStream(destino));

            if (comprimido) salida.setMethod(ZipOutputStream.DEFLATED);

            byte datos[] = new byte[BUFFER];

            for (int x = 0; x < path_ficheros.length; x++) {
                FileInputStream entrada = new FileInputStream(path_ficheros[x]);
                origen = new BufferedInputStream(entrada, BUFFER);
                ZipEntry zipeadao = new ZipEntry(Normalizar(nombre_ficheros[x]));
                salida.putNextEntry(zipeadao);
                int num_datos;
                while ((num_datos = origen.read(datos, 0, BUFFER)) != -1)
                    salida.write(datos, 0, num_datos);
                origen.close();
            }
            salida.close();
        } catch (FileNotFoundException e) {
            throw new FicheroException();
        } catch (IOException e) {
            throw new FicheroException();
        }
    }

    private String Normalizar(String nombre_fichero) {
        nombre_fichero = nombre_fichero.toUpperCase();
        nombre_fichero = nombre_fichero.replaceAll("Á", "A");
        nombre_fichero = nombre_fichero.replaceAll("É", "E");
        nombre_fichero = nombre_fichero.replaceAll("Í", "I");
        nombre_fichero = nombre_fichero.replaceAll("Ó", "O");
        nombre_fichero = nombre_fichero.replaceAll("Ú", "U");
        nombre_fichero = nombre_fichero.replaceAll("Ñ", "N");
        return nombre_fichero;
    }

}